﻿using System.Collections.Generic;

namespace AreaConverterNamespace 
{
    public class AreaConverter
    {
        private readonly Dictionary<string, double> allAvailableMeasures;

        public AreaConverter()
        {
            allAvailableMeasures = new Dictionary<string, double>
                {
                    {"Meters", AreaConstants.METER},
                    {"Santimeters", AreaConstants.SANTIMETER},
                    {"Millimeters", AreaConstants.MILLIMETER},
                    {"Kilometers", AreaConstants.KILOMETER},
                    {"Hectares", AreaConstants.HECTARE},
                    {"Ars", AreaConstants.AR},
                };
        }

        public double GetMeasureType(string measureName)
        {
            if (allAvailableMeasures.ContainsKey(measureName))
                return allAvailableMeasures[measureName];
            throw new UndefinedMeasureTypeException();
        }

        public int GetAvailableMeasuresCount()
        {
            return allAvailableMeasures.Count;
        }

        public double Convert(Measure knownMeasure, Measure wantedMeasure)
        {
            double value = knownMeasure.amount*knownMeasure.measureInMeters/wantedMeasure.measureInMeters;
            wantedMeasure.amount = value;
            return value;
        }

        public double Convert(string knownMeasureName, string wantedMeasureName, double knownMeasureAmount)
        {
            double knownMeasureInMeter = GetMeasureType(knownMeasureName);
            double wantedMeasureInMeter = GetMeasureType(wantedMeasureName);
            var knownMeasure = new Measure(knownMeasureInMeter, knownMeasureAmount);
            var wantedMeasure = new Measure(wantedMeasureInMeter);
            return Convert(knownMeasure, wantedMeasure);
        }

        public List<string> GetAvailableMeasureNames()
        {
            return new List<string>(allAvailableMeasures.Keys);
        }
    }
}
